/**
 * 
 */
package dms.dto;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import dms.model.Faculty;
import dms.service.SchoolService;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class ClassesDTO extends ActionForm {
	private Long id;
	private Long faculty;
	private String name;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the faculty
	 */
	public Long getFaculty() {
		return faculty;
	}
	/**
	 * @param faculty the faculty to set
	 */
	public void setFaculty(Long faculty) {
		this.faculty = faculty;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (this.name == null || this.name.length() == 0) {
			errors.add("errors", new ActionMessage("缺少班级名称", false));
		}
		
		if (this.faculty == null || this.faculty == 0) {
			errors.add("errors", new ActionMessage("缺少科系", false));

		}
		
		if (errors.size() != 0 && this.faculty != null) {
			SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
			Faculty faculty = schoolService.findFacultyById(this.faculty);
			request.setAttribute("faculty", faculty);
		}
		return errors;
	}
	
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.name = null;
		this.faculty = null;
		this.id = null;
	}
}
